import direction
import pygame

class Actor(pygame.sprite.Sprite):
	def __init__(self, actorImage, initialPosition, initalVelocity, stepLength):
		pygame.sprite.Sprite.__init__(self)
		
		# The sprite representing the player
		self.image = actorImage
		self.rect = self.image.get_rect()
		self.rect.topleft = initialPosition
		
		# The direction the player is facing
		self.Orientation = direction.Down
	
		# Boolean variable to track whether or not the player is currently moving
		self._moving = False
		
		# Boolean to track if the player is moving between two points
		self._takingStep = False
		
		self._stepLength = stepLength
		
		# The player velocity in pixels per second
		self._velocity = initalVelocity
		
		# For the internal update code we need to keep track of velocity as the number of 
		# milliseconds until we move one pixel
		self._millisecondsPerPixelMoved = 1000 / self._velocity
		
		# Internal variable to keep track of the passage of time so that we know when to update
		self._millisecondsTilUpdate = self._millisecondsPerPixelMoved
		
		# A list of rectangles which the player cannot move through
		self.ImpassableRectangles = []
		
	def SetVelocity(self, newVelocity):
		self._velocity = newVelocity
		self._millisecondsPerPixelMoved = 1000 / self._velocity
	
	def GetVelocity(self):
		return self._velocity
		
	def GetPosition(self):
		# The overall position of the player is the middle of the sprite rectangle
		xPos = self.rect.left + self.rect.width / 2
		yPos = self.rect.top + self.rect.height / 2
		
		return [xPos, yPos]
		
	def SetPosition(self, newPosition):	
		# Center the sprite rectangle around the new position
		self.rect = pygame.Rect(
			self._position[0] - self.rect.width / 2, 
			self._position[1] - self.rect.height / 2, 
			self.rect.width, 
			self.rect.height)
		
	def MoveLeft(self):
		self._moving = True
		self._takingStep = True
		self._target = self.rect.move([-self._stepLength, 0])
		self.Orientation = direction.Left
		
	def MoveRight(self):
		self._moving = True
		self._takingStep = True
		self._target = self.rect.move([self._stepLength, 0])
		self.Orientation = direction.Right
	
	def MoveUp(self):
		self._moving = True
		self._takingStep = True
		self._target = self.rect.move([0, -self._stepLength])
		self.Orientation = direction.Up
	
	def MoveDown(self):
		self._moving = True
		self._takingStep = True
		self._target = self.rect.move([0, self._stepLength])
		self.Orientation = direction.Down
	
	def StopMoving(self):
		self._moving = False
		self._takingStep = False
		self._millisecondsTilUpdate = self._millisecondsPerPixelMoved

	def Update(self, elapsedMilliseconds, impassableRectangles):
		if self._moving:
			# If we're moving start counting down how long until we need to update the position
			self._millisecondsTilUpdate -= elapsedMilliseconds
			
			self.ImpassableRectangles = impassableRectangles
			
			# Once the counter drops below zero, we want to update, and add the amount of time
			# until another update back onto the counter.  If for some reason a great deal of
			# time passed since the last update, the counter could drop far enough below zero
			# that updating the position once wouldn't be sufficient.  This is why we use a
			# while loop here.
			while(self._millisecondsTilUpdate < 0):
				# We're going to try and update the position.  Keep track of where we are now
				# in case we collide with an object after moving and we need to undo the move.
				previousPosition = self.rect
				
				DirectionVector = []
				if(self.Orientation == direction.Left):
					DirectionVector = [-1, 0]
				elif(self.Orientation == direction.Right):
					DirectionVector = [1, 0]
				elif(self.Orientation == direction.Up):
					DirectionVector = [0, -1]
				elif(self.Orientation == direction.Down):
					DirectionVector = [0, 1]
				else:
					raise StandardError('Player orientation is set to an invalid value')
					
				self.rect = self.rect.move(DirectionVector)
				
				if self.rect == self._target:
					self.StopMoving()
					self._target = [-1,-1]
				
				if(self.rect.collidelist(self.ImpassableRectangles) != -1):
					self.StopMoving()
					self.rect = previousPosition
				
				self._millisecondsTilUpdate += self._millisecondsPerPixelMoved
			
	def Draw(self, screen):
		screen.blit(self.image, self.rect.topleft)